package com.example.biaodashijisuan;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;

public class MainActivity extends AppCompatActivity {
    Stack<String> stack = new Stack();
    String a = "";//随机生成的中缀表达式
    String result = "";//转换后的后缀表达式
    String process1 = "";//转换过程中的过程存量
    String process2 = "";//计算后缀的过程存量
    EditText editText_randominfix;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button result = findViewById(R.id.button);
        result.setOnClickListener(new myButtonListener2());

        Button button1 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(new myButtonListener());

        Button button2 = (Button) findViewById(R.id.button3);
        button2.setOnClickListener(new myButtonListener1());


    }
    //看到中缀转后缀的具体过程。
           public class myButtonListener implements View.OnClickListener {
                @Override
                public void onClick(View v) {
                    EditText editText3 = (EditText) findViewById(R.id.editText3);
                    ArrayList<String> expre = new ArrayList<String>();
                    a = editText_randominfix.getText().toString();
                    editText3.setText(a,TextView.BufferType.EDITABLE);
                    Stack op = new Stack();
                    StringTokenizer tokenizer = new StringTokenizer(a);

                    while (tokenizer.hasMoreTokens()) {
                        a = tokenizer.nextToken();

                        if (a.equals("("))
                        {op.push(a);         // 如果是"("，入栈
                        process1 +=  "进入符号栈的是："+op.peek()+"\n";
                        editText3.setText(process1,TextView.BufferType.EDITABLE);}
                        else if (a.equals("+") || a.equals("-")) {
                            if (!op.isEmpty()) {
                                if (op.peek().equals("(")){
                                    op.push(a);     // 如果栈顶"("，运算符入栈
                                    process1 +=  "进入符号栈的是："+op.peek()+"\n";
                                    editText3.setText(process1,TextView.BufferType.EDITABLE); }
                                else {
                                    expre.add((String) op.pop());        // 先移除栈顶元素到列表中，再将运算符入栈
                                    op.push(a);
                                    process1 +=  "进入符号栈的是："+op.peek()+"\n";
                                    editText3.setText(process1,TextView.BufferType.EDITABLE);
                                }
                            } else {
                                op.push(a);     // 栈为空，运算符入栈
                                process1 +=  "进入符号栈的是："+op.peek()+"\n";
                                editText3.setText(process1,TextView.BufferType.EDITABLE);
                            }
                        } else if (a.equals("*") || a.equals("÷")) {
                            if (!op.isEmpty()) {
                                if (op.peek().equals("*") || op.peek().equals("÷")) {
                                    expre.add((String) op.pop());
                                    op.push(a);
                                    process1 +=  "进入符号栈的是："+op.peek()+"\n";
                                    editText3.setText(process1,TextView.BufferType.EDITABLE);
                                } else
                                    op.push(a);
                                process1 +=  "进入符号栈的是："+op.peek()+"\n";
                                editText3.setText(process1,TextView.BufferType.EDITABLE);
                            } else
                                op.push(a); // 如果栈为空，运算符入栈
                            process1 +=  "进入符号栈的是："+op.peek()+"\n";
                            editText3.setText(process1,TextView.BufferType.EDITABLE);
                        } else if (a.equals(")")) {
                            while (true) {
                                String b = (String) op.pop();
                                if (!b.equals("(")) {
                                    expre.add(b);
                                } else {
                                    break;
                                }
                            }
                        } else {
                            expre.add(a);  // 如果为操作数，入列表
                            process1 +=  "进入操作数栈的是："+ expre.get(expre.size()-1) +"\n";
                            editText3.setText(process1,TextView.BufferType.EDITABLE);
                        }
                    }
                    while (!expre.isEmpty() && !op.isEmpty()) {
                        expre.add((String) op.pop());
                    }

                    for (String x : expre)
                        result += x + " ";

                }
    }


           //看后缀表达式计算。
          public  class myButtonListener1 implements View.OnClickListener {
              @Override
              public void onClick(View v) {
                  final EditText editText7 = (EditText) findViewById(R.id.editText7);
                  class Comparision {
                      Stack<String> stack = new Stack<String>();
                      RationalNumber num1,num2,num3;

                      public Comparision() {
                          stack = new Stack<String>();
                      }

                      public String calculator(String q)
                      {
                          String op1, op2, result;
                          String a;
                          StringTokenizer tokenizer = new StringTokenizer(q);

                          while (tokenizer.hasMoreTokens())
                          {
                              a = tokenizer.nextToken();

                              if(a.equals("+") || a.equals("-") || a.equals("*") || a.equals("÷"))
                              {
                                  // 把栈顶的操作数弹出，转换为RationalNumber类型
                                  op2 = stack.pop();
                                  process2 +=  "弹出的操作数是"+op2+"\n";
                                  num2 = conToRantionalNum(op2);


                                  // 继续把栈顶的操作数弹出，转换为RationalNumber类型
                                  op1 = stack.pop();
                                  process2 +=  "弹出的操作数是"+op1+"\n";
                                  num1 = conToRantionalNum(op1);

                                  //计算结果
                                  result = calculate(num1,a.charAt(0),num2);
                                  stack.push(result.toString());
                                  process2 +=  "结果是："+result.toString()+"\n";
                              }
                              else
                                  stack.push(a);
                          }
                          result = stack.pop();
                          return result;
                      }
                      public RationalNumber conToRantionalNum (String a)
                      {
                          String numer,denom;  // numer分子，denom分母


                          //  把分数a/b以"/"为标记分割开
                          StringTokenizer tokenizer1 = new StringTokenizer(a,"/");
                          numer = tokenizer1.nextToken();
                          if (tokenizer1.hasMoreTokens())
                          //  如果分母不为1
                          {
                              denom = tokenizer1.nextToken();
                              num1 = new RationalNumber(Integer.parseInt(numer), Integer.parseInt(denom));
                          }
                          else
                          //  如果分母为1
                          {
                              num1 = new RationalNumber(Integer.parseInt(numer), 1);
                          }
                          return num1;
                      }

                      // 计算+、-、*、÷
                      public String calculate(RationalNumber op1, char operation, RationalNumber op2)
                      {
                          RationalNumber result = new RationalNumber(0,1);

                          switch (operation)
                          {
                              case '+':
                                  result = op1.add(op2);
                                  break;
                              case '-':
                                  result = op1.subtract(op2);
                                  break;
                              case '*':
                                  result = op1.multiply(op2);
                                  break;
                              case '÷':
                                  result = op1.divide(op2);
                                  break;
                          }
                          return result.toString();
                      }
                  }
                  editText7.setText(process1,TextView.BufferType.EDITABLE);
              }
          }


            public class myButtonListener2 implements View.OnClickListener{
             @Override
            public void onClick(View v) {
                 editText_randominfix = findViewById(R.id.editText);
                 final EditText editText_suffix = findViewById(R.id.editText2);
                 final EditText editText_result = findViewById(R.id.editText4);
                 String infix = editText_randominfix.getText().toString();
                //生成中缀表达式
                 class Arithmetic {

                    char[] operator = {'+', '-', '*', '÷'};
                    Random generator = new Random();
                    int num1, num2, num3, a;
                    private String result="";
                     public String level() {
                         num1 = generator.nextInt(20)+1;
                         num2 = generator.nextInt(20)+1;
                         num3 = generator.nextInt(2) + 2;
                         a = generator.nextInt(3);
                         result = num1 + " " + operator[num3] + " " + num2;
                         for (int x = 0; x <= a; x++) {
                             num2 = generator.nextInt(20)+1;
                             num3 = generator.nextInt(4);
                             result += " " + operator[num3] + " " + num2;
                         }
                         return result;
                     }

                    //  重写toString方法。
                    public String toString(){
                        String result = "";
                        result = this.result;
                        return result;
                    }
                }
                Arithmetic randominfix = new Arithmetic();
                 infix = randominfix.level();
                 editText_randominfix.setText(infix);

                 String suffix;
                //中缀表达式转后缀表达式。
                class Convert {
                    List<String> expre;   // expression 表达式
                    Stack<String> op;   // 操作符
                    String result = "";

                    public Convert() {
                        expre = new ArrayList<String>();
                        op = new Stack();
                    }

                    // 中缀表达式转后缀表达式的转换方法。
                    public String transit(String q)  // question
                    {
                        String a;           // a 是字符
                        StringTokenizer tokenizer = new StringTokenizer(q);

                        while (tokenizer.hasMoreTokens()) {
                            a = tokenizer.nextToken();

                            if (a.equals("("))
                                op.push(a);         // 如果是"("，入栈
                            else if (a.equals("+") || a.equals("-")) {
                                if (!op.isEmpty()) {
                                    if (op.peek().equals("("))
                                        op.push(a);     // 如果栈顶"("，运算符入栈
                                    else {
                                        expre.add(op.pop());        // 先移除栈顶元素到列表中，再将运算符入栈
                                        op.push(a);
                                    }
                                } else {
                                    op.push(a);     // 栈为空，运算符入栈
                                }
                            } else if (a.equals("*") || a.equals("÷")) {
                                if (!op.isEmpty()) {
                                    if (op.peek().equals("*") || op.peek().equals("÷")) {
                                        expre.add(op.pop());
                                        op.push(a);
                                    } else
                                        op.push(a);
                                } else
                                    op.push(a); // 如果栈为空，运算符入栈
                            } else if (a.equals(")")) {
                                while (true) {
                                    String b = op.pop();
                                    if (!b.equals("(")) {
                                        expre.add(b);
                                    } else {
                                        break;
                                    }
                                }
                            } else {
                                expre.add(a);  // 如果为操作数，入列表
                            }
                        }
                        while (!expre.isEmpty() && !op.isEmpty()) {
                            expre.add(op.pop());
                        }

                        for (String x : expre)
                            result += x + " ";
                        return result;
                    }
                }
                Convert convert = new Convert();
                suffix = convert.transit(infix);
                editText_suffix.setText(suffix);

                //后缀表达式计算方法
                class Comparision {
                    Stack<String> stack = new Stack<String>();

                    RationalNumber num1,num2,num3;

                    public Comparision() {
                        stack = new Stack<String>();
                    }

                    public String calculator(String q)
                    {
                        String op1, op2, result;
                        String a;
                        StringTokenizer tokenizer = new StringTokenizer(q);

                        while (tokenizer.hasMoreTokens())
                        {
                            a = tokenizer.nextToken();

                            if(a.equals("+") || a.equals("-") || a.equals("*") || a.equals("÷"))
                            {
                                // 把栈顶的操作数弹出，转换为RationalNumber类型
                                op2 = stack.pop();
                                num2 = conToRantionalNum(op2);

                                // 继续把栈顶的操作数弹出，转换为RationalNumber类型
                                op1 = stack.pop();
                                num1 = conToRantionalNum(op1);

                                //计算结果
                                result = calculate(num1,a.charAt(0),num2);
                                stack.push(result.toString());
                            }
                            else
                                stack.push(a);
                        }
                        result = stack.pop();
                        return result;
                    }

                    public RationalNumber conToRantionalNum (String a)
                    {
                        String numer,denom;  // numer分子，denom分母


                        //  把分数a/b以"/"为标记分割开
                        StringTokenizer tokenizer1 = new StringTokenizer(a,"/");
                        numer = tokenizer1.nextToken();
                        if (tokenizer1.hasMoreTokens())
                        //  如果分母不为1
                        {
                            denom = tokenizer1.nextToken();
                            num1 = new RationalNumber(Integer.parseInt(numer), Integer.parseInt(denom));
                        }
                        else
                        //  如果分母为1
                        {
                            num1 = new RationalNumber(Integer.parseInt(numer), 1);
                        }
                        return num1;
                    }

                    // 计算+、-、*、÷
                    public String calculate(RationalNumber op1, char operation, RationalNumber op2)
                    {
                        RationalNumber result = new RationalNumber(0,1);

                        switch (operation)
                        {
                            case '+':
                                result = op1.add(op2);
                                break;
                            case '-':
                                result = op1.subtract(op2);
                                break;
                            case '*':
                                result = op1.multiply(op2);
                                break;
                            case '÷':
                                result = op1.divide(op2);
                                break;
                        }
                        return result.toString();
                    }
                }
                String result;
                Comparision woresult = new Comparision();
                result = woresult.calculator(suffix);
                editText_result.setText(result);
            }
        };
    }





